﻿using System;
using Acaweb.Data.Contracts;
using Acaweb.Model.Entities;
using Acaweb.Model.Entities.DatosCompuestos;
using Acaweb.Componentes.Contracts;
using System.Collections.Generic;
using System.Linq;

namespace Acaweb.Componentes
{
    public class HistorialLaboralService : BaseService, IHistorialLaboralService
    {
        private IHistorialLaboralDao _HistorialLaboralDao;

        public IHistorialLaboralDao HistorialLaboralDao
        {
            get { return _HistorialLaboralDao; }
            set { _HistorialLaboralDao = value; }
        }

        public IList<HistorialLaboral> GetAll()
        {
            return HistorialLaboralDao.GetAll();
        }

        public HistorialLaboral GetById(HistorialLaboralPk HistorialLaboralPk)
        {
            return HistorialLaboralDao.GetById(HistorialLaboralPk);
        }

        public void Create(HistorialLaboral HistorialLaboral)
        {

            HistorialLaboral.HistorialLaboralPk.Secuencia = GetNextSecuencia(HistorialLaboralDao.GetByAsociado(HistorialLaboral.HistorialLaboralPk.CodigoInterno).ToList());
            
            HistorialLaboralDao.Save(HistorialLaboral);
        }

        public void Delete(HistorialLaboralPk HistorialLaboral)
        {
            HistorialLaboralDao.Delete(HistorialLaboral);
        }

        public void Update(HistorialLaboral HistorialLaboral)
        {
            HistorialLaboralDao.Update(HistorialLaboral);
        }

        private int GetNextSecuencia(List<HistorialLaboral> Lista)
        {
            int Secuencia = 1;

            if(Lista != null){
                if (Lista.Count > 0)
                {
                    foreach (HistorialLaboral HL in Lista)
                    {
                        if (HL.HistorialLaboralPk.Secuencia >= Secuencia)
                        {
                            Secuencia = HL.HistorialLaboralPk.Secuencia + 1;
                        }
                    }
                }
            }

            return Secuencia;

        }
    }
}